package cv.cvs.Servive;

import cv.cvs.dao.CardMapper;
import cv.cvs.pojo.Card;
import cv.cvs.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class CardService implements CardMapper {
    @Override
    public List<Card> getCardList() {
        SqlSession sqlSession = null;
        List<Card> cardList = new ArrayList<Card>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            cardList = sqlSession.getMapper(CardMapper.class).getCardList();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return cardList;
    }

    @Override
    public Card getCardIdNumber(String idNumber) {
        SqlSession sqlSession = null;
        Card card = new Card();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            card = sqlSession.getMapper(CardMapper.class).getCardIdNumber(idNumber);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return card;
    }

    @Override
    public int addCard(Card card) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(CardMapper.class).addCard(card);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    @Override
    public int modify(Integer status, Integer id) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(CardMapper.class).modify(status, id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    @Override
    public int deleteCard(Integer id) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(CardMapper.class).deleteCard(id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }
}
